import gnotifier, pygtk, os, sys, gtk, gobject, webbrowser, re, httplib, xml.dom.minidom, time
APP_DIR = gnotifier.APP_DIR

class GNmodule:
    lastCheck = None
    menu = None

    def __init__(self, pluginManager):
        self.pluginManager = pluginManager

        self.icon = gtk.StatusIcon()
        self.icon.set_from_file(APP_DIR+self.statusicon)
        gtk.window_set_default_icon_from_file(APP_DIR+'icon.png')

        self.gmailFeed = gnotifier.googleFeedManager(self.service, self.feedUri)
        self._generateMenu()
        self.icon.connect("popup-menu", self.popupmenu, self.menu) 
        self.icon.connect("activate", self.getMail)
    
    def loginInto(self, user='', passwd=''):
        self.lastChecked = None
        print 'U: '+user+' P:'+passwd
        if (self.gmailFeed.getAuthToken(user, passwd) != 200):
          print "Error login into GMail"
        else:
          self.__execute()

    def _generateMenu(self):    
        return False;

    def _populateMenu(self):    
        return False;

    def _checkNewMail(self):    
        return False;

    def getMail(self, a=''):
        gtk.gdk.flush()
        print "entering getMail..."
        self.gmailFeed.getFeed()
        print '1'
        self._checkNewMail()
        print '2'
        self._populateMenu()
        print '3'      
        return True

    def openUrl(self, widget, url):
        print 'Opening ' + url
        webbrowser.open(url)

    def popupmenu(self, widget, button, time, data = None):
     if button == 3:
         if data:
             data.show_all()
             data.popup(None, None, None, 3, time)
     pass

    def __execute(self):
        self.getMail()       
        self.timeout = gobject.timeout_add(60000*5, self.getMail)

